#include "sendfile.h"

#include <QFile>
#include <QFileInfo>
#include <QHostAddress>

SendFile::SendFile(QObject *parent) : QObject(parent)
{

}

void SendFile::connectServer(unsigned short port, QString ip)
{
    m_tcp = new QTcpSocket;
    m_tcp->connectToHost(QHostAddress(ip), port);

    connect(m_tcp, &QTcpSocket::connected, this, &SendFile::connectOK);
    connect(m_tcp, &QTcpSocket::disconnected, this, [=]()
    {
        m_tcp->close();
        m_tcp->deleteLater();
        emit gameover();
    });
}

void SendFile::sendFile(QString path)
{
    if(m_tcp == nullptr)
    {
        qDebug() << "请先建立连接！！";
        return;
    }
    QFile file(path);
    file.open(QFile::ReadOnly);

    QFileInfo info(path);
    int fileSize = info.size();

    while (!file.atEnd())
    {
        static int num = 0;
        if(num == 0)
        {
            m_tcp->write((char*)&fileSize, 4);
        }
        QByteArray line = file.readLine();
        num += line.size();
        int percent = num * 100 / fileSize;
        emit curPercent(percent);

        m_tcp->write(line);
    }
}
